/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-06-24 14:34:57
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-10-16 09:42:03
 * @FilePath: \keep_motion_api\controllers\user\user.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { User } = require('@/models/user');
const multer = require('multer');
const path = require('path');
const fs = require('fs');

// 配置 multer 存储引擎
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    const uploadDir = './uploads';
    if (!fs.existsSync(uploadDir)) {
      fs.mkdirSync(uploadDir);
    }
    cb(null, uploadDir);
  },
  filename: function (req, file, cb) {
    const ext = path.extname(file.originalname);
    cb(null, `${Date.now()}${ext}`);
  }
});
const upload = multer({ storage: storage });


const getUsers = async (req, res) => {
  try {
    const { name, phone, status, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await User.getUsers(name, phone, status, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询用户失败', error });
  }
}

const createUser = async (req, res) => {
  try {
    const { username, password, name, phone, email, avatar, status } = req.body;
    const user = await User.createUser(username, password, name, phone, email, avatar, status);
    res.status(200).send(user);
  } catch (error) {
    res.status(500).json({ message: '创建用户失败', error });
  }
}

const updateUser = async (req, res) => {
  try {
    const { id } = req.params;
    const { username, password, name, phone, email, avatar, status } = req.body;
    const user = await User.updateUser(id, username, password, name, phone, email, avatar, status);
    res.status(200).send(user);
  } catch (error) {
    res.status(500).json({ message: '更新用户失败', error });
  }
}

const deleteUser = async (req, res) => {
  try {
    const { id } = req.params;
    await User.deleteUser(id);
    res.status(200).send({ message: '删除用户成功' });
  } catch (error) {
    res.status(500).json({ message: '删除用户失败', error });
  }
}
const updateUserAvatar = async (req, res) => {
  try {
      upload.single('file')(req, res, async (err) => {
        if (err) {
          return res.status(500).json({ message: '头像上传失败', error: err });
        }
        const file = req.file;
        if (!file) {
          return res.status(400).json({ message: '未上传头像' });
        }
        const { id } = req.params;
        const user = await User.updateUserAvatar(id, file.path);
        res.status(200).send({message:'头像上传成功', file:user.avatar});
      });
    } catch (error) {
      res.status(500).json({ message: '更新用户头像失败', error });
    }
}
const wxLogin = async (req, res) => {
  try {
    const { code } = req.query;
    const user = await User.wxLogin(code);
    res.status(200).send(user);
  } catch (error) {
    res.status(500).json({ message: '微信登录失败', error });
  }
}

module.exports = {
  getUsers,
  createUser,
  updateUser,
  deleteUser,
  updateUserAvatar,
  wxLogin
}
